//
//  HTTPHepler.m
//  Food choices
//
//  Created by xalo on 16/2/26.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "HTTPHepler.h"

#import "SubCategory.h"     //分类
#import "TGCategory.h"
#import "CityModel.h"       //城市
#import "DistrictModel.h"   //行政区
#import "Biz_areaModel.h"   //商圈
#import "SelectDealModel.h" //筛选到的团单
#import "SelectShopModel.h" //筛选的商户
#import "ShopModel.h"       //商户
#import "DealModel.h"       //团单

@interface HTTPHepler ()
//商户下团单列表
@property (nonatomic,strong) NSMutableArray *shopDealsArray;

//商户详情
@property (nonatomic,strong) NSMutableArray *shopModelArray; //一个元素

//团单详情
@property (nonatomic,strong) NSMutableArray *dealModelArray; //一个元素

//城市数组
@property (nonatomic,strong) NSMutableArray *cityArray;

//行政区数组
@property (nonatomic,strong) NSMutableArray *districtArray;

//分类信息
@property (nonatomic,strong) NSMutableArray *TGcategoryArray; //一个元素

//根据团单id查询的商户列表
@property (nonatomic,strong) NSMutableArray *shopsArray;

//根据查询条件筛选的商户
@property (nonatomic,strong) NSMutableArray *selectShopModelArray; //一个元素

//根据查询条件筛选的团单
@property (nonatomic,strong) NSMutableArray *selectDealModelArray; //一个元素

@end


static HTTPHepler *handle = nil;

//主引擎
@implementation HTTPHepler

+ (HTTPHepler *)sharedManager{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[HTTPHepler alloc] init];
    });
    
    return handle;
}

//请求数据
- (void)fetchDataWithUrl:(NSString *)url parameter:(NSMutableDictionary *)dict species:(NSInteger)species
{
    
    //定义回调
    APISCallBack *callBack = [APISCallBack alloc];
    
    callBack.onSuccess =  ^(long status, NSString* responseString) {
        NSLog(@"onSuccess");
        if(responseString != nil) {
            //成功返回数据后，解析数据
            [self analyticalDataWithString:responseString species:species];
        }
    };
    
    callBack.onError = ^(long status, NSString* responseString) {
        NSLog(@"onError");
        
    };
    
    callBack.onComplete = ^(){
        NSLog(@"onComplete");
        
    };
    
    //固定参数
    NSString *method = @"get";
    
    NSString *apikey = @"3fab788fcbbf413b17cd00ed233d79c7";
    
    //请求API
    [ApiStoreSDK executeWithURL:url method:method apikey:apikey parameter:dict callBack:callBack];
    
}


//返回数组数量
- (NSInteger)countofArrayWithSpecies:(NSInteger)species
{
    NSArray *array = [self arryWithSpecies:species];
    return array.count;
}


//返回对应的model
- (instancetype)modelOfSpecies:(NSInteger)species index:(NSInteger)index
{
    NSArray *array = [self arryWithSpecies:species];
    return  array[index];
}


//根据species返回对应的数组
- (NSMutableArray *)arryWithSpecies:(NSInteger)species
{
    switch (species) {
        case 0:
            return self.shopDealsArray;
            break;
            
        case 1:
            return self.shopModelArray;
            break;
            
        case 2:
            return self.dealModelArray;
            break;
            
        case 3:
            return self.cityArray;
            break;
            
        case 4:
            return self.districtArray;
            break;
        case 5:
            return self.TGcategoryArray;
            break;
        case 6:
            return self.shopsArray;
            break;
        case 7:
            return self.selectShopModelArray;
            break;
        case 8:
            return self.selectDealModelArray;
            break;
            
        default:
            break;
    }
    return nil;
}


//解析数据
- (void)analyticalDataWithString:(NSString *)string species:(NSInteger)species
{
    //数据转换为NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    //跟数组
    NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
    
    switch (species) {
            
            //商户下团单列表
        case 0:
        {
            //数组置空
            [self.shopDealsArray removeAllObjects];
            
            NSArray *dealArray = rootDic[@"deals"];
            
            for (NSDictionary *dealDic in dealArray) {
                DealModel *dealModel = [[DealModel alloc] init];
                
                [dealModel setValuesForKeysWithDictionary:dealDic];
                
                [self.shopDealsArray addObject:dealModel];
            }
            
            break;
            
        }
            
            //商户详情
        case 1:
        {
            //model数组置空
            [self.shopModelArray removeAllObjects];
            
            ShopModel *shopModel = [[ShopModel alloc] init];
            [shopModel setValuesForKeysWithDictionary:rootDic[@"shop"]];
            
            [self.shopModelArray addObject:shopModel];
            
            break;
        }
            
            //团单详情
        case 2:
        {
            //model数组置空
            [self.dealModelArray removeAllObjects];
            
            DealModel *dealModel = [[DealModel alloc] init];
            
            [dealModel setValuesForKeysWithDictionary:rootDic[@"deal"]];
            
            [self.dealModelArray addObject:dealModel];
            
            break;
        }
            
            //城市列表
        case 3:
        {
            NSArray *city = rootDic[@"cities"];
            for (NSDictionary *cityDic in city) {
                CityModel *cityModel = [[CityModel alloc] init];
                [cityModel setValuesForKeysWithDictionary:cityDic];
                
                [self.cityArray addObject:cityModel];
            }
            break;
        }
            
            //行政区列表
        case 4:
        {
            //置空
            [self.districtArray removeAllObjects];
            
            NSArray *districts = rootDic[@"districts"];
            for (NSDictionary *disDic in districts) {
                DistrictModel *districtModel = [[DistrictModel alloc] init];
                [districtModel setValuesForKeysWithDictionary:disDic];
                
                [self.districtArray addObject:districtModel];
            }
            
            break;
        }
            
            //分类信息
        case 5:
        {
            //置空
            [self.TGcategoryArray removeAllObjects];
            
            TGCategory *tgCategory = [[TGCategory alloc] init];
            [tgCategory setValuesForKeysWithDictionary:rootDic[@"categories"]];
            [self.TGcategoryArray addObject:tgCategory];
            
            break;
        }
            
            //根据团单id查询的商户列表
        case 6:
        {
            //数组置空
            [self.shopsArray removeAllObjects];
            
            NSArray *shopArray = rootDic[@"shops"];
            
            for (NSDictionary *shopDic in shopArray) {
                ShopModel *shopModel = [[ShopModel alloc] init];
                
                [shopModel setValuesForKeysWithDictionary:shopDic];
                
                [self.shopsArray addObject:shopModel];
            }
            
            break;
        }
            
            //根据查询条件获取团单
        case 7:
        {
            //置空
            [self.selectDealModelArray removeAllObjects];
            
            SelectDealModel *selectDealModel = [[SelectDealModel alloc] init];
            
            [selectDealModel setValuesForKeysWithDictionary:rootDic[@"data"]];
            
            [self.selectShopModelArray addObject:selectDealModel];
            
            break;
        }
            
            //根据查询条件筛选的商户
        case 8:
        {
            //置空
            [self.selectShopModelArray removeAllObjects];
            SelectShopModel *selectShopModel = [[SelectShopModel alloc] init];
            
            [selectShopModel setValuesForKeysWithDictionary:rootDic[@"data"]];
            
            [self.selectShopModelArray addObject:selectShopModel];
            
            break;
        }
        default:
            break;
    }
}


//懒加载
- (NSMutableArray *)shopDealsArray
{
    if (!_shopDealsArray) {
        self.shopDealsArray = [NSMutableArray array];
    }
    return _shopDealsArray;
}


- (NSMutableArray *)cityArray
{
    if (!_cityArray) {
        self.cityArray = [NSMutableArray array];
    }
    return _cityArray;
}

- (NSMutableArray *)districtArray
{
    if (!_districtArray) {
        self.districtArray = [NSMutableArray array];
    }
    return _districtArray;
}

-(NSMutableArray *)TGcategoryArray
{
    if (!_TGcategoryArray) {
        self.TGcategoryArray = [NSMutableArray array];
    }
    return _TGcategoryArray;
}

- (NSMutableArray *)shopsArray
{
    if (!_shopsArray) {
        self.shopsArray = [[NSMutableArray alloc] init];
        
    }
    return _shopsArray;
}

-(NSMutableArray *)selectShopModelArray
{
    if (!_selectShopModelArray) {
        self.selectShopModelArray = [NSMutableArray array];
    }
    return _selectShopModelArray;
}

-(NSMutableArray *)selectDealModelArray
{
    if (!_selectDealModelArray) {
        self.selectDealModelArray = [NSMutableArray array];
    }
    return _selectDealModelArray;
}

-(NSMutableArray *)shopModelArray
{
    if (!_shopModelArray) {
        self.shopModelArray = [NSMutableArray array];
    }
    return _shopModelArray;
}

-(NSMutableArray *)dealModelArray
{
    if (!_dealModelArray) {
        self.dealModelArray = [NSMutableArray array];
    }
    return _dealModelArray;
}

@end
